package com.cmc.cloud.cmclink.doc.util;

import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.util.json.JsonUtils;
import com.cmes.framework.security.core.LoginUser;
import com.cmes.framework.security.core.util.SecurityFrameworkUtils;
import com.cmes.platform.system.api.mail.MailSendApi;
import com.cmes.platform.system.api.mail.dto.MailTemplateSendReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author zhouxian 2025/3/11
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class EmailUtil {

    @Autowired
    private MailSendApi mailSendApi;

    /**
     * sendMail
     *
     * @param email          email
     * @param templateCode   templateCode
     * @param templateParams templateParams
     */
    public void sendMail(String email, String templateCode, Map<String, Object> templateParams) {
        sendMail(email, templateCode, templateParams, null);
    }

    /**
     * sendMail
     *
     * @param email          email
     * @param templateCode   templateCode
     * @param templateParams templateParams
     * @param attachments    attachments
     */
    public void sendMail(String email, String templateCode, Map<String, Object> templateParams,
                         List<MultipartFile> attachments) {
        // 发送邮件必须要有登录态
        LoginUser originLoginUser = SecurityFrameworkUtils.getLoginUser();

        SecurityFrameworkUtils.setNoneHttpLoginUser(originLoginUser);
        log.info("Sending email to: {}, templateCode: {}, templateParams: {}", email, templateCode,
                JsonUtils.toJsonString(templateParams));
        MailTemplateSendReqDTO reqDto = new MailTemplateSendReqDTO();
        reqDto.setMail(email);
        reqDto.setTemplateCode(templateCode);
        reqDto.setTemplateParams(JsonUtils.toJsonString(templateParams));
        reqDto.setAttachments(attachments);
        mailSendApi.sendMail(reqDto);
        if (originLoginUser != null) {
            SecurityFrameworkUtils.setNoneHttpLoginUser(originLoginUser);
        }
    }

    /**
     * @param email          发送地址
     * @param emailCc        抄送地址
     * @param templateCode   邮件模板
     * @param templateParams 邮件填充数据
     */
    public void spcSendMail(String email, String emailCc, String templateCode, Map<String, Object> templateParams,
                            List<MultipartFile> attachments) {
        LoginUser originLoginUser = SecurityFrameworkUtils.getLoginUser();
        SecurityFrameworkUtils.setNoneHttpLoginUser(originLoginUser);
        MailTemplateSendReqDTO reqDto = new MailTemplateSendReqDTO();
        reqDto.setMail(email);
        reqDto.setCc(emailCc);
        reqDto.setTemplateCode(templateCode);
        reqDto.setTemplateParams(JsonUtils.toJsonString(templateParams));
        //reqDto.setAttachments(attachments);
        CommonResult<Long> longCommonResult = null;
        try {
            longCommonResult = mailSendApi.sendMail(reqDto);
        } catch (Exception e) {
            log.error("spcSendMailEmailCcError:{},spcSendMailEmailCc: {},{}, templateCode: {}", e.getMessage(), email, emailCc, templateCode);
        }
        log.info("spcSendMailEmailResult: {},{}, templateCode: {}, templateParams: {},longCommonResult:{}", email, emailCc, templateCode,
                JsonUtils.toJsonString(templateParams), JsonUtils.toJsonString(longCommonResult));
        if (originLoginUser != null) {
            SecurityFrameworkUtils.setNoneHttpLoginUser(originLoginUser);
        }
    }
}
